package org.grow.secure.controller;

import org.grow.secure.entity.TestLeave;
import org.grow.secure.entity.UserInfo;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDate;

/**
 * @Author: xwg
 * @CreateDate: 21-7-15
 */

@RestController
public class TestController {


    @GetMapping("/testMvc")
    public String testMvc(){
        return LocalDate.now().toString();
    }

//   url: applyLeave post

//   req: {"days":2,"reason":"游山玩水","type":"yearLeave"}
//   res : boolean
    @PostMapping("/applyLeave")
    public Boolean applyLeave(@RequestBody TestLeave testLeave, HttpServletRequest request){
        System.out.println("假设保存了请假信息： "+testLeave);

        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        UserInfo principal = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        System.out.println("请假人："+auth.getName());
        System.out.println("请假人："+principal.getUsername());
        return true;
    }
}
